export function _ajaxTool(method, url, data) {
    return new Promise(function (resolve, reject) {
        let xhr = new XMLHttpRequest();
        if (method == "GET") {
            //对data操作形成新的url
            let str = ''
            for (const key in data) {
                str += `${key}=${data[key]}&`
            }
            let newUrl = `${url}?${str.slice(0, -1)}`
            xhr.open(method, newUrl)
            // xhr.setRequestHeader('')
            xhr.send()
        }
        else {
            let postData = ""
            xhr.open(method, url)
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
            //对data操作    形成    新的字符串
            for (const key in data) {
                postData += `${key}=${data[key]}&`
            }
            postData = postData.slice(0, -1)
            xhr.send(postData)
        }

        xhr.addEventListener('readystatechange', function () {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    resolve(JSON.parse(xhr.responseText))
                }
                else {
                    reject('请求有问题，请检查参数或者地址是否正确')
                }

            }
        })
    })
}